package com.ehualu.getes.controller;

import com.ehualu.getes.service.EsService;
import com.ehualu.getes.until.OracleUtills;
import com.ehualu.getes.until.TimeUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping(value = "/restes")
public class Controllers {


    @Autowired
    private EsService esService;


    @RequestMapping(value = "/getinfo", method = RequestMethod.GET)
    @ResponseBody
    public int getinfo() {


        String host = "37.158.97.130,37.158.97.131,37.158.97.132";
        int port = 24148;
        String schema = "http";


//        int num = esService.getnum(host, port, schema);
//
        System.out.println("******num************:");
        return port;


    }


    /*
     * 过车速度
     * */
    @RequestMapping(value = "/getsd", method = RequestMethod.GET)
    @ResponseBody
    public String getsd(String dw1kkid1, String dw1kkid2, String dw2kkid1, String dw2kkid2) {


//        if (betime>0){
//
//            System.out.println("betime："+betime);
//
//            System.out.println("betime 大于0不合规");
//
//        }


        System.out.println("dw1kkid1:" + dw1kkid1);
        System.out.println("dw1kkid2:" + dw1kkid2);

        System.out.println("dw2kkid1:" + dw2kkid1);

        System.out.println("dw2kkid2:" + dw2kkid2);

        String kkid1[] = new String[2];
        String kkid2[] = new String[2];


        kkid1[0] = dw1kkid1;
        kkid2[0] = dw2kkid1;

        if (dw1kkid2.equals("0")) {

            System.out.println("dw1kkid2 不传");
        } else {

            kkid1[1] = dw1kkid2;

        }

        if (dw2kkid2.equals("0")) {

            System.out.println("dw2kkid2 不传");
        } else {

            kkid2[1] = dw2kkid2;

        }


        int betime = -30;


        SimpleDateFormat simt = new SimpleDateFormat("yyyyMM");


        Date date1 = new Date();

        String timed = simt.format(date1);

//        System.out.println(timed);


        String index = "guoche_" + timed;
        System.out.println("ES索引：" + index);


//        esService.gcsd()

//        int num = esService.getnum(host, port, schema);

        String pjsd = esService.gcsd(index, kkid1, kkid2, betime);

        System.out.println("******pjsd************:" + pjsd);
        return pjsd;


    }


    /*
     * 过车时间
     * */
    @RequestMapping(value = "/getsj", method = RequestMethod.GET)
    @ResponseBody
    public String getsj(String dw1kkid1, String dw1kkid2, String dw2kkid1, String dw2kkid2) {

        System.out.println("dw1kkid1:" + dw1kkid1);
        System.out.println("dw1kkid2:" + dw1kkid2);

        System.out.println("dw2kkid1:" + dw2kkid1);

        System.out.println("dw2kkid2:" + dw2kkid2);

        String kkid1[] = new String[2];
        String kkid2[] = new String[2];

        kkid1[0] = dw1kkid1;
        kkid2[0] = dw2kkid1;

        if (dw1kkid2.equals("0")) {

            System.out.println("dw1kkid2 不传");
        } else {

            kkid1[1] = dw1kkid2;

        }

        if (dw2kkid2.equals("0")) {

            System.out.println("dw2kkid2 不传");
        } else {

            kkid2[1] = dw2kkid2;

        }

        int betime = -30;

        SimpleDateFormat simt = new SimpleDateFormat("yyyyMM");


        Date date1 = new Date();

        String timed = simt.format(date1);

        String index = "guoche_" + timed;
        System.out.println("ES索引：" + index);


        String pjsd = esService.gcsj(index, kkid1, kkid2, betime);

        System.out.println("******pjsd************:" + pjsd);
        return pjsd;

    }


    /*
     * 当日流量统计
     * */
    @RequestMapping(value = "/gettj", method = RequestMethod.GET)
    @ResponseBody
    public String gettj(String dltype, String dlbh, String fx, String nowtime, String timetype, String cattype) {


        if (cattype.equals("nocattype")) {


            System.out.println("gettj  nowtime" + nowtime);
            long nowtimelong = new Long(nowtime);
            Date date = new Date(nowtimelong);

            TimeUntil timeUntil = new TimeUntil();


            String startime = "";
            String endtime = "";

            if (timetype.equals("D")) {

                System.out.println("按天统计");
                SimpleDateFormat simda = new SimpleDateFormat("yyyy-MM-dd'T'");
//        设置时区

                String resda = simda.format(date);
                String resdastar = resda + "00:00:00.000Z";
                String resdaend = resda + "23:59:59.999Z";

                System.out.println("********当前时间天开始*********" + resdastar);
                System.out.println("********当前时间天结束*********" + resdaend);
                startime = timeUntil.dateToStringtuic(resdastar);

                endtime = timeUntil.dateToStringtuic(resdaend);


            } else if (timetype.equals("H")) {


                System.out.println("按小时统计");

                SimpleDateFormat simh = new SimpleDateFormat("yyyy-MM-dd'T'HH");
                String resh = simh.format(date);
                String reshstart = resh + ":00:00.000Z";
                String reshend = resh + ":59:59.999Z";


                System.out.println("********当前时间小时开始*********" + reshstart);
                System.out.println("********当前时间小时结束*********" + reshend);
                startime = timeUntil.dateToStringtuic(reshstart);

                endtime = timeUntil.dateToStringtuic(reshend);


            } else {

                System.out.println("timetype违法");

                return "0";

            }
            System.out.println("********统计开始时间*********" + startime);
            System.out.println("********统计结束时间*********" + endtime);


            SimpleDateFormat indexsimt = new SimpleDateFormat("yyyyMM");


            String indeshouz = indexsimt.format(date);


            String index = "guoche_" + indeshouz;
            System.out.println("ES索引：" + index);


            System.out.println("dltype:" + dltype);

            System.out.println("dlbh:" + dlbh);
            System.out.println("fx:" + fx);

            String strllct = esService.getliulct(index, startime, endtime, dltype, dlbh, fx);

            return strllct;


        } else if (cattype.equals("whp")) {

            String strllct = getwhptj(dltype, dlbh, fx, nowtime, timetype);
            return strllct;

        } else {
            System.out.println("cattype 不合法");


            return "0";
        }


    }

    /*
     *
     * 当日流量函数
     * */
    public String gettjfuc(String dltype, String dlbh, String fx, String nowtime, String timetype, List<String> listdldm) {


        System.out.println("gettj  nowtime" + nowtime);
        long nowtimelong = new Long(nowtime);
        Date date = new Date(nowtimelong);

        TimeUntil timeUntil = new TimeUntil();


        String startime = "";
        String endtime = "";

        if (timetype.equals("D")) {

            System.out.println("按天统计");
            SimpleDateFormat simda = new SimpleDateFormat("yyyy-MM-dd'T'");
//        设置时区

            String resda = simda.format(date);
            String resdastar = resda + "00:00:00.000Z";
            String resdaend = resda + "23:59:59.999Z";

            System.out.println("********当前时间天开始*********" + resdastar);
            System.out.println("********当前时间天结束*********" + resdaend);
            startime = timeUntil.dateToStringtuic(resdastar);

            endtime = timeUntil.dateToStringtuic(resdaend);


        } else if (timetype.equals("H")) {


            System.out.println("按小时统计");

            SimpleDateFormat simh = new SimpleDateFormat("yyyy-MM-dd'T'HH");
            String resh = simh.format(date);
            String reshstart = resh + ":00:00.000Z";
            String reshend = resh + ":59:59.999Z";


            System.out.println("********当前时间小时开始*********" + reshstart);
            System.out.println("********当前时间小时结束*********" + reshend);
            startime = timeUntil.dateToStringtuic(reshstart);

            endtime = timeUntil.dateToStringtuic(reshend);


        } else {

            System.out.println("timetype违法");

            return "0";

        }
        System.out.println("********统计开始时间*********" + startime);
        System.out.println("********统计结束时间*********" + endtime);


        SimpleDateFormat indexsimt = new SimpleDateFormat("yyyyMM");


        String indeshouz = indexsimt.format(date);


        String index = "guoche_" + indeshouz;
        System.out.println("ES索引：" + index);


        System.out.println("dltype:" + dltype);

        System.out.println("dlbh:" + dlbh);
        System.out.println("fx:" + fx);

        String strllct = esService.getliulct7d(index, startime, endtime, dltype, dlbh, fx, listdldm);

        return strllct;


    }


    /*
     *
     *七天道路流量统计
     * */
    @RequestMapping(value = "/gettj7d", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, String> gettj7d(String dltype, String dlbh, String fx, String nowtime, String timetype, String cattype) {


        if (cattype.equals("nocattype")) {


            SimpleDateFormat simh = new SimpleDateFormat("yyyy-MM-dd");


            Map<String, String> mapct = new HashMap<>();

            OracleUtills oracleUtills = new OracleUtills();

            List<String> listdldm = oracleUtills.getDldm(dltype, dlbh);

            Long nowtimelong = new Long(nowtime);

            String strnowtime = nowtime;
            for (int d = 0; d < 7; d++) {

                System.out.println(nowtimelong);
                System.out.println(strnowtime);


                String strct = gettjfuc(dltype, dlbh, fx, strnowtime, timetype, listdldm);


                Date date = new Date(nowtimelong);
                String tjsj = simh.format(date);
                System.out.println("********tjsj*********" + tjsj);
                mapct.put(tjsj, strct);

                Calendar cald = Calendar.getInstance();
                cald.setTime(date);
                cald.add(Calendar.DATE, -1);
                Date adddated = cald.getTime();
                nowtimelong = adddated.getTime();

                strnowtime = nowtimelong.toString();

            }

            return mapct;
//

        } else if (cattype.equals("whp")) {


            return getwhptj7d(dltype, dlbh, fx, nowtime, timetype);


        } else {
            System.out.println("cattype 不合法");

            return null;

        }


    }


    /*
     * 每日危化品统计接口
     *
     * */
    @RequestMapping(value = "/getwhptj", method = RequestMethod.GET)
    @ResponseBody
    public String getwhptj(String dltype, String dlbh, String fx, String nowtime, String timetype) {


        System.out.println("gettj  nowtime" + nowtime);
        long nowtimelong = new Long(nowtime);
        Date date = new Date(nowtimelong);

        TimeUntil timeUntil = new TimeUntil();


        String startime = "";
        String endtime = "";

        if (timetype.equals("D")) {

            System.out.println("按天统计");
            SimpleDateFormat simda = new SimpleDateFormat("yyyy-MM-dd'T'");
//        设置时区

            String resda = simda.format(date);
            String resdastar = resda + "00:00:00.000Z";
            String resdaend = resda + "23:59:59.999Z";

            System.out.println("********当前时间天开始*********" + resdastar);
            System.out.println("********当前时间天结束*********" + resdaend);
            startime = timeUntil.dateToStringtuic(resdastar);

            endtime = timeUntil.dateToStringtuic(resdaend);


        } else if (timetype.equals("H")) {


            System.out.println("按小时统计");

            SimpleDateFormat simh = new SimpleDateFormat("yyyy-MM-dd'T'HH");
            String resh = simh.format(date);
            String reshstart = resh + ":00:00.000Z";
            String reshend = resh + ":59:59.999Z";


            System.out.println("********当前时间小时开始*********" + reshstart);
            System.out.println("********当前时间小时结束*********" + reshend);
            startime = timeUntil.dateToStringtuic(reshstart);

            endtime = timeUntil.dateToStringtuic(reshend);


        } else {

            System.out.println("timetype违法");

            return "0";

        }
        System.out.println("********统计开始时间*********" + startime);
        System.out.println("********统计结束时间*********" + endtime);


        SimpleDateFormat indexsimt = new SimpleDateFormat("yyyyMM");


        String indeshouz = indexsimt.format(date);


        String index = "guoche_" + indeshouz;
        System.out.println("ES索引：" + index);


        System.out.println("dltype:" + dltype);

        System.out.println("dlbh:" + dlbh);
        System.out.println("fx:" + fx);

        String strllct = esService.getliulwhp(index, startime, endtime, dltype, dlbh, fx);

        return strllct;


    }


    /*
     * 每日危化品统计函数
     *
     * */
    public Double getwhptjfuc(String dltype, String dlbh, String fx, String nowtime, String timetype, List<String> listdldm, String[] szwhpcp, String[] whpcplx) {


        System.out.println("gettj  nowtime" + nowtime);
        long nowtimelong = new Long(nowtime);
        Date date = new Date(nowtimelong);

        TimeUntil timeUntil = new TimeUntil();


        String startime = "";
        String endtime = "";

        if (timetype.equals("D")) {

            System.out.println("按天统计");
            SimpleDateFormat simda = new SimpleDateFormat("yyyy-MM-dd'T'");
//        设置时区

            String resda = simda.format(date);
            String resdastar = resda + "00:00:00.000Z";
            String resdaend = resda + "23:59:59.999Z";

            System.out.println("********当前时间天开始*********" + resdastar);
            System.out.println("********当前时间天结束*********" + resdaend);
            startime = timeUntil.dateToStringtuic(resdastar);

            endtime = timeUntil.dateToStringtuic(resdaend);


        } else if (timetype.equals("H")) {


            System.out.println("按小时统计");

            SimpleDateFormat simh = new SimpleDateFormat("yyyy-MM-dd'T'HH");
            String resh = simh.format(date);
            String reshstart = resh + ":00:00.000Z";
            String reshend = resh + ":59:59.999Z";


            System.out.println("********当前时间小时开始*********" + reshstart);
            System.out.println("********当前时间小时结束*********" + reshend);
            startime = timeUntil.dateToStringtuic(reshstart);

            endtime = timeUntil.dateToStringtuic(reshend);


        } else {

            System.out.println("timetype违法");

            return 0.0;

        }
        System.out.println("********统计开始时间*********" + startime);
        System.out.println("********统计结束时间*********" + endtime);


        SimpleDateFormat indexsimt = new SimpleDateFormat("yyyyMM");


        String indeshouz = indexsimt.format(date);


        String index = "guoche_" + indeshouz;
        System.out.println("ES索引：" + index);


        System.out.println("dltype:" + dltype);

        System.out.println("dlbh:" + dlbh);
        System.out.println("fx:" + fx);

        Double strllct = esService.getliulwhp7d(index, startime, endtime, dltype, dlbh, fx, listdldm, szwhpcp, whpcplx);

        return strllct;


    }


    /*
     *
     *七天危化品流量统计
     * */
    @RequestMapping(value = "/getwhptj7d", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, String> getwhptj7d(String dltype, String dlbh, String fx, String nowtime, String timetype) {
        SimpleDateFormat simh = new SimpleDateFormat("yyyy-MM-dd");


        Map<String, String> mapct = new HashMap<>();


        Long nowtimelong = new Long(nowtime);

        String strnowtime = nowtime;

        OracleUtills oracleUtills = new OracleUtills();

        List<String> listdldm = oracleUtills.getDldm(dltype, dlbh);

        List<String> listwhpcp = oracleUtills.getwhpcp();

        String[] szwhpcp = listwhpcp.toArray(new String[listwhpcp.size()]);


        List<String> listwhpcphc = oracleUtills.getwhpcphc();

        List<String> listwhpcppt = oracleUtills.getwhpcppt();


        String[] szwhpcphc = listwhpcphc.toArray(new String[listwhpcphc.size()]);

        String[] szwhpcppt = listwhpcppt.toArray(new String[listwhpcppt.size()]);


        String whphcsz[] = new String[2];

        whphcsz[0] = "01";
        whphcsz[1] = "15";

        String whpptsz[] = new String[1];

        whpptsz[0] = "02";


        for (int d = 0; d < 7; d++) {

            System.out.println(nowtimelong);
            System.out.println(strnowtime);


            Double doucthc = getwhptjfuc(dltype, dlbh, fx, strnowtime, timetype, listdldm, szwhpcphc, whphcsz);

            Double douctpt = getwhptjfuc(dltype, dlbh, fx, strnowtime, timetype, listdldm, szwhpcppt, whpptsz);


            Double douctsum = doucthc + douctpt;
            System.out.println("doucthc:" + doucthc);

            System.out.println("douctpt:" + douctpt);

            System.out.println("douctsum:" + douctsum);


            String strsumct = douctsum.toString();
            Date date = new Date(nowtimelong);
            String tjsj = simh.format(date);
            System.out.println("********tjsj*********" + tjsj);
            mapct.put(tjsj, strsumct);

            Calendar cald = Calendar.getInstance();
            cald.setTime(date);
            cald.add(Calendar.DATE, -1);
            Date adddated = cald.getTime();
            nowtimelong = adddated.getTime();

            strnowtime = nowtimelong.toString();

        }

        return mapct;
//

    }


    /*
     *
     * 根据车尾找车牌
     * */
    @RequestMapping(value = "/getcarnum", method = RequestMethod.GET)
    @ResponseBody
//    carnumtype  车牌是头还是尾   t  是头 。w  是尾
    public Map<String, List<String>> getcarnum(String carnumtype, String guahoucarnum, int gcfw, Integer timcs) {


        SimpleDateFormat simt = new SimpleDateFormat("yyyyMM");


        Date date1 = new Date();

        String timed = simt.format(date1);

//        System.out.println(timed);
        System.out.println("车头车尾类型：" + carnumtype);
        System.out.println("时间窗口：" + timcs);
        String index = "guoche_" + timed;
        System.out.println("ES索引：" + index);
        System.out.println("挂车车牌：" + guahoucarnum);



        Map<String, List<String>> guaheadmap = esService.getguacarnum(carnumtype, index, guahoucarnum, gcfw, timcs);

        return guaheadmap;


    }


}
